package cn.edu.sctu.sctu_lysd.view.act;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.Required;

import java.util.ArrayList;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.biz.GoodsMessageBiz;
import cn.edu.sctu.sctu_lysd.databinding.ActivityGoodsMessageBinding;
import cn.edu.sctu.sctu_lysd.presenter.GoodsMessagePresenter;
import cn.edu.sctu.sctu_lysd.utils.ImageLoader;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.utils.L;

/**
 * Created by wangfuxin on 2017-04-09.
 */

public class GoodsMessageAct extends BaseActivity implements GoodsMessageBiz.GoodsMessageView, Validator.ValidationListener {

    private ActivityGoodsMessageBinding mBinding;

    private GoodsMessageBiz.Presenter mPresenter;

    @Required(order = 1, message = "必填选项！")
    private EditText parcelName;
    @Required(order = 2, message = "必填选项！")
    private EditText parcelWeight;
    @Required(order = 3, message = "必填选项！")
    private EditText parcelDescribe;

    public final static String NAME = "parcelName";
    public final static String WEIGHT = "parcelWeight";
    public final static String DESCRIBE = "parcelDescribe";
    public final static String IMG_PATH = "img_path";
    private final static int RESULT_CODE = 1100;

    public final static int PHOTO_REQUERS_CODE = 8520;

    private Validator validator;

    private String imgPath = "";

    // 要申请的权限
    private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_goods_message;
    }

    @Override
    protected void initAfter(Object bind) {

        mBinding = (ActivityGoodsMessageBinding) bind;

        parcelName = mBinding.goodsName;
        parcelWeight = mBinding.goodsWeight;
        parcelDescribe = mBinding.goodsDescribe;

        initPresenter(new GoodsMessagePresenter(this, this));

        validator = new Validator(this);

        validator.setValidationListener(this);

        mBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                validator.validate();
            }
        });

        mBinding.selectPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 检查该权限是否已经获取
                int i = ContextCompat.checkSelfPermission(GoodsMessageAct.this, permissions[0]);
                // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
                if (i != PackageManager.PERMISSION_GRANTED) {
                    // 如果没有授予该权限，就去提示用户请求
                    ActivityCompat.requestPermissions(GoodsMessageAct.this, permissions, 321);
                } else {
                    Intent intent = new Intent(GoodsMessageAct.this, PhotoActivity.class);

                    startActivityForResult(intent, PHOTO_REQUERS_CODE);

                }

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        L.e("path", resultCode + "-------" + requestCode);

        if (resultCode == RESULT_OK && requestCode == PHOTO_REQUERS_CODE) {

            Bundle bundle = data.getExtras();
            ArrayList<String> stringArray = bundle.getStringArrayList(PhotoActivity.IMAGE_PATH_INTENT_FLAG);

            imgPath = stringArray.get(0);

            ImageLoader.getInstance().loadImage(stringArray.get(0), mBinding.selectPhoto, false);


        }


    }

    @Override
    public void initPresenter(GoodsMessageBiz.Presenter presenter) {
        this.mPresenter = presenter;

        mPresenter.start();

    }

    @Override
    public void uploadSuccess() {

    }

    @Override
    public void uploadFailed() {

    }

    @Override
    public void showSelectPhoto(String[] path) {

        ImageLoader.getInstance().loadImage(path[0], mBinding.selectPhoto, false);

    }

    @Override
    public void onValidationSucceeded() {

        Intent intent = new Intent(GoodsMessageAct.this, SubmitReleaseAct.class);
        Bundle bundle = new Bundle();
        bundle.putString(NAME, mPresenter.getInputSting(parcelName));
        bundle.putString(WEIGHT, mPresenter.getInputSting(parcelWeight));
        bundle.putString(DESCRIBE, mPresenter.getInputSting(parcelDescribe));
        bundle.putString(IMG_PATH, imgPath);
        intent.putExtras(bundle);
        if (!imgPath.equals("")) {
            setResult(RESULT_OK, intent);
            finish();
        } else {
            Toast.makeText(this, "请选择一张图片", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onValidationFailed(View failedView, Rule<?> failedRule) {

        String message = failedRule.getFailureMessage();

        if (failedView instanceof EditText) {
            failedView.requestFocus();
            ((EditText) failedView).setError(message);
        } else {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }

    }
}
